/*
 * @Author: heinan
 * @Date: 2023-07-27 20:34:27
 * @Last Modified by: heinan
 * @Last Modified time: 2023-07-27 20:38:15
 */
const http = require("http");
const NodeMediaServer = require("node-media-server");
const { Server } = require("socket.io");

const app = require("express")();
const httpServer = http.createServer(app);

class LiveService {
  constructor(app) {
    this.app = app;
    this.roomList = {};
    this.userList = [];
  }
  init() {
    if (!this.app.nms) {
      const that = this;
      // 创建nms服务，获取nms配置对象
      this.app.nms = new NodeMediaServer(this.app.config.mediaServer);
      // 创建socket服务
      const io = new Server(httpServer, {
        cors: true,
      });
      httpServer.listen(3000, () => {
        console.log("server is running at http://127.0.0.1:3000");
      });

      io.on("connection", (socket) => {
        // 用户加入
        socket.on("join", function (user) {
          // 通过emit定向广播
          if (that.userList.includes(user)) return;
          that.userList.push(user);
          socket.emit("announcement", `"用户 ${user} 加入了聊天室!`);
        });
        socket.on("send.message", function (user, msg) {
          // 通过全局广播
          console.log(msg);
          io.emit("send.message", user, msg);
        });
        socket.on("disconnect", function () {
          if (socket.nickname) {
            socket.broadcast.emit(
              "send.message",
              "用户",
              socket.nickname + "已离开聊天室!"
            );
          }
        });
      });

      this.app.nms.run();
      this.app.nms.on("preConnect", (id, args) => {
        console.log(
          "[NodeEvent on preConnect]",
          `id=${id} args=${JSON.stringify(args)}`
        );
        // let session = nms.getSession(id);
        // session.reject();
      });

      this.app.nms.on("postConnect", (id, args) => {
        console.log(
          "[NodeEvent on postConnect]",
          `id=${id} args=${JSON.stringify(args)}`
        );
      });

      this.app.nms.on("doneConnect", (id, args) => {
        console.log(
          "[NodeEvent on doneConnect]",
          `id=${id} args=${JSON.stringify(args)}`
        );
      });

      this.app.nms.on("prePublish", (id, StreamPath, args) => {
        console.log(
          "[NodeEvent on prePublish]",
          `id=${id} StreamPath=${StreamPath} args=${JSON.stringify(args)}`
        );
        // let session = nms.getSession(id);
        // session.reject();
      });

      this.app.nms.on("postPublish", (id, StreamPath, args) => {
        console.log(
          "[NodeEvent on postPublish]",
          `id=${id} StreamPath=${StreamPath} args=${JSON.stringify(args)}`
        );
      });

      this.app.nms.on("donePublish", (id, StreamPath, args) => {
        console.log(
          "[NodeEvent on donePublish]",
          `id=${id} StreamPath=${StreamPath} args=${JSON.stringify(args)}`
        );
      });

      this.app.nms.on("prePlay", (id, StreamPath, args) => {
        console.log(
          "[NodeEvent on prePlay]",
          `id=${id} StreamPath=${StreamPath} args=${JSON.stringify(args)}`
        );
        // let session = nms.getSession(id);
        // session.reject();
      });

      this.app.nms.on("postPlay", (id, StreamPath, args) => {
        console.log(
          "[NodeEvent on postPlay]",
          `id=${id} StreamPath=${StreamPath} args=${JSON.stringify(args)}`
        );
      });

      this.app.nms.on("donePlay", (id, StreamPath, args) => {
        console.log(
          "[NodeEvent on donePlay]",
          `id=${id} StreamPath=${StreamPath} args=${JSON.stringify(args)}`
        );
      });
    }
  }
}

module.exports = LiveService;
